package cn.xshi.oauth.service.impl;

import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.oauth.dao.OauthTenantDao;
import cn.xshi.oauth.model.OauthTenant;
import cn.xshi.oauth.param.OauthAccountTenantParam;
import cn.xshi.oauth.service.OauthAccountTenantService;
import cn.xshi.oauth.service.OauthTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 * @Desc 租户
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
@Slf4j
public class OauthTenantServiceImpl extends BaseService implements OauthTenantService {

    @Resource
    OauthTenantDao oauthTenantDao;

    @Resource
    OauthAccountTenantService oauthAccountTenantService;

    /**
     * 分页
     * @param condition
     * @return
     */
    public List<OauthTenant> getOauthTenantListByCondition(Map<String, Object> condition){
        return oauthTenantDao.getOauthTenantListByCondition(condition);
    }

    /**
     * 查询对象
     * @param id
     * @return
     */
    public OauthTenant getOauthTenantById(String id){
        return oauthTenantDao.getOauthTenantById(id);
    }

    /**
     * 添加
     * @param oauthTenant
     * @return
     */
    public int addOauthTenant(OauthTenant oauthTenant){
        oauthTenant.setId(toUUID());
        oauthTenant.setCreateTime(getDate());
        oauthTenant.setCreateId(getCurrentAccountId());
        return oauthTenantDao.addOauthTenant(oauthTenant);
    }

    /**
     * 修改
     * @param oauthTenant
     * @return
     */
    public int updateOauthTenant(OauthTenant oauthTenant){
        oauthTenant.setUpdateTime(getDate());
        oauthTenant.setUpdateId(getCurrentAccountId());
        return oauthTenantDao.updateOauthTenant(oauthTenant);
    }

    /**
     * 删除
     * @param condition
     * @return
     */
    public int delOauthTenant(Map<String, Object> condition){
        OauthAccountTenantParam oauthAccountTenantParam = new OauthAccountTenantParam();
        oauthAccountTenantParam.setBatchTenantId((String[]) condition.get("id"));
        Integer count = oauthAccountTenantService.getOauthAccountTenantCount(oauthAccountTenantParam);
        if(count>0){
            throw new ExceptionUtil("存在租户被绑定！");
        }
        return oauthTenantDao.delOauthTenant(condition);
    }

    /**
     * 根据账号类型查询租户
     * @param condition
     * @return
     */
    public List<OauthTenant> getOauthTenantListByAccountTypeId(Map<String, Object> condition){
        return oauthTenantDao.getOauthTenantListByAccountTypeId(condition);
    }
}
